﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Crude.Core
{
    public class CrudeSettingsStateLoader
    {
        const string CrudeLocalPathVariable = "CrudeLocalPath";
        const string CrudeRemotePathVariable = "CrudeRemotePath";
        const string CrudeBinPath = "CrudeBinPath";

        public CrudeSettingsState Load()
        {
            string localPath = 
                Environment.GetEnvironmentVariable(CrudeLocalPathVariable, EnvironmentVariableTarget.Process);

            if (string.IsNullOrEmpty(localPath))
            {
                localPath =
                    Environment.GetEnvironmentVariable(CrudeLocalPathVariable, EnvironmentVariableTarget.User);

                if (string.IsNullOrEmpty(localPath))
                {
                    localPath =
                        Environment.GetEnvironmentVariable(CrudeLocalPathVariable, EnvironmentVariableTarget.Machine);

                }
            }

            string remotePath =
                Environment.GetEnvironmentVariable(CrudeRemotePathVariable, EnvironmentVariableTarget.Process);

            if (string.IsNullOrEmpty(localPath))
            {
                remotePath =
                    Environment.GetEnvironmentVariable(CrudeRemotePathVariable, EnvironmentVariableTarget.User);

                if (string.IsNullOrEmpty(localPath))
                {
                    remotePath =
                        Environment.GetEnvironmentVariable(CrudeRemotePathVariable, EnvironmentVariableTarget.Machine);

                }
            }

            string crudeBinPath =
                 Environment.GetEnvironmentVariable(CrudeBinPath, EnvironmentVariableTarget.Process);

            if (string.IsNullOrEmpty(localPath))
            {
                crudeBinPath =
                    Environment.GetEnvironmentVariable(CrudeBinPath, EnvironmentVariableTarget.User);

                if (string.IsNullOrEmpty(localPath))
                {
                    crudeBinPath =
                        Environment.GetEnvironmentVariable(CrudeBinPath, EnvironmentVariableTarget.Machine);

                }
            }
                                
            return new CrudeSettingsState
            {
                LocalRepositoryPath = localPath,
                RemoteRepositoryPath = remotePath,
                CrudeBinPath = crudeBinPath
            };
        }
    }
}
